/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.boot.redis;

import com.iwindplus.boot.redis.aspect.RedisRateLimiterAspect;
import com.iwindplus.boot.redis.domain.property.RedisProperty;
import com.iwindplus.boot.redis.service.impl.ClientIpRateLimiterKeyResolver;
import com.iwindplus.boot.redis.service.impl.DefaultRateLimiterKeyResolver;
import com.iwindplus.boot.redis.service.impl.ServerNodeRateLimiterKeyResolver;
import com.iwindplus.boot.redis.service.impl.UserRateLimiterKeyResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * Redis限流配置.
 *
 * @author zengdegui
 * @since 2018/9/5
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({RedisProperty.class})
@ConditionalOnProperty(prefix = "spring.redis.rate-limiter", name = "enabled", havingValue = "true", matchIfMissing = true)
public class RedisRateLimiterConfiguration {

    /**
     * 创建 RedisRateLimiterAspect.
     *
     * @return RedisRateLimiterAspect
     */
    @Bean
    public RedisRateLimiterAspect redisRateLimiterAspect() {
        RedisRateLimiterAspect redisRateLimiterAspect = new RedisRateLimiterAspect();
        log.info("RedisRateLimiterAspect={}", redisRateLimiterAspect);
        return redisRateLimiterAspect;
    }

    /**
     * 创建 DefaultRateLimiterKeyResolver.
     *
     * @return DefaultRateLimiterKeyResolver
     */
    @Primary
    @Bean
    public DefaultRateLimiterKeyResolver defaultRateLimiterKeyResolver() {
        DefaultRateLimiterKeyResolver defaultRateLimiterKeyResolver = new DefaultRateLimiterKeyResolver();
        log.info("DefaultRateLimiterKeyResolver={}", defaultRateLimiterKeyResolver);
        return defaultRateLimiterKeyResolver;
    }

    /**
     * 创建 UserRateLimiterKeyResolver.
     *
     * @return UserRateLimiterKeyResolver
     */
    @Bean
    public UserRateLimiterKeyResolver userRateLimiterKeyResolver() {
        UserRateLimiterKeyResolver userRateLimiterKeyResolver = new UserRateLimiterKeyResolver();
        log.info("UserRateLimiterKeyResolver={}", userRateLimiterKeyResolver);
        return userRateLimiterKeyResolver;
    }

    /**
     * 创建 ClientIpRateLimiterKeyResolver.
     *
     * @return ClientIpRateLimiterKeyResolver
     */
    @Bean
    public ClientIpRateLimiterKeyResolver clientIpRateLimiterKeyResolver() {
        ClientIpRateLimiterKeyResolver clientIpRateLimiterKeyResolver = new ClientIpRateLimiterKeyResolver();
        log.info("ClientIpRateLimiterKeyResolver={}", clientIpRateLimiterKeyResolver);
        return clientIpRateLimiterKeyResolver;
    }

    /**
     * 创建 ServerNodeRateLimiterKeyResolver.
     *
     * @return ServerNodeRateLimiterKeyResolver
     */
    @Bean
    public ServerNodeRateLimiterKeyResolver serverNodeRateLimiterKeyResolver() {
        ServerNodeRateLimiterKeyResolver serverNodeRateLimiterKeyResolver = new ServerNodeRateLimiterKeyResolver();
        log.info("ServerNodeRateLimiterKeyResolver={}", serverNodeRateLimiterKeyResolver);
        return serverNodeRateLimiterKeyResolver;
    }
}
